<?php
/*
* Description: Users class
*/
class Users extends DbBase{

	var $id;
	var $username;
	var $password;
	var $name;
	var $address;
	var $hometown;
	var $current_city;
	var $gender;
	var $birthdate;
	var $about_me;
	var $profile_picture;
	var $relationship_status;
	var $email_ad1;
	var $email_ad2;
	var $phone_number1;
	var $phone_number2;
	var $website;
	
    // constructor
	public function __construct() {
        $this->table_name = 'users';
    }
	
	//authorize if user exists
	public function authorize($user, $password) {
		$sql  = "SELECT count(*) as count ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE username='$user' AND password='$password'";
		
		$result = array_shift($this->query($sql));
		if($result['count']>0)
			return true;
		return false;
	}
	
	public function find_by_username($username){
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		$sql .= " WHERE username='$username'";
		
		$result = array_shift($this->query($sql));
		$this->construct_attributes($result);
	}
	
	public function find_all(){
		$result = array();
		
		$sql  = "SELECT * ";
		$sql .= " FROM ".$this->table_name;
		
		$rows = $this->query($sql);
		foreach($rows as $index => $row){
			$user = new Users();
			$user->set_attributes($row);
			$result[$index] = $user;
		}
		
		return $result;
	}
	
	public function update(){ 
		$this->validate(array('email_ad1' => 'Email'));
		
		$sql  = "UPDATE ".$this->table_name;
		$sql .= " SET";
		
		$attrs = get_class_vars(get_class($this));
		unset($attrs['id']);
		unset($attrs['table_name']);
		
		foreach($attrs as $key => $value){
			if(!is_null($this->$key) && $this->$key != '')
				$sql .= " $key = '".$this->$key."',";
		}
		$sql  = rtrim($sql,',');
		$sql .= " WHERE id = $this->id";
		
		return $this->execute($sql);
	}
	
	public function insert(){
		$this->validate(array('email_ad1' => 'Email', 'password' => 'Password'));
	
		$sql   = "INSERT INTO $this->table_name ";
		$sql  .= "(username, password, name, address, ";
		$sql  .= "hometown, current_city, gender, birthdate, ";
		$sql  .= "about_me, profile_picture, email_ad1, ";
		$sql  .= "email_ad2, phone_number1, phone_number2, ";
		$sql  .= "website, relationship_status)";
		$sql  .= " VALUES";
		$sql  .= "('$this->username', '$this->password', '$this->name', '$this->address',";
		$sql  .= "'$this->hometown', '$this->current_city', '$this->gender', '$this->birthdate',";
		$sql  .= "'$this->about_me', '$this->profile_picture', '$this->email_ad2',";
		$sql  .= "'$this->email_ad2', '$this->phone_number1', '$this->phone_number2',";
		$sql  .= "'$this->website', '$this->relationship_status')";
		
		return $this->execute($sql);
	}
	
	public function delete(){
		$sql  = "DELETE FROM $this->table_name ";
		$sql .= "WHERE id = $this->id";
		return $this->execute($sql);
	}
	
	// CLASS METHODS section
	public static function get_all_users(){
		$user = new Users();
		return $user->find_all();
	}
	
	public static function delete_user($user_id){
		$user = new Users();
		$user->id = $user_id;
		return $user->delete();
	}
	
	public function select(){ throw new Exception("method unimplemented yet"); }

}
?>